package com.weipai;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2017/11/10 0010.
 */
public class ServerHttp {

    public  boolean SendMessage(String url ,String list){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");

        // 解决中文乱码问题
        StringEntity stringEntity = new StringEntity(list, "UTF-8");
        stringEntity.setContentEncoding("UTF-8");

        httpPost.setEntity(stringEntity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
          String out =   getContent(response);
            if(out.indexOf("html")!=-1){
                return false;
            }
            if(out.indexOf("error")!=-1){
                return false;
            }
            System.out.println(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }


    public String getContent(CloseableHttpResponse response){
        ByteArrayOutputStream baos   =   new   ByteArrayOutputStream();
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream instream = null;
            try {
                instream = entity.getContent();
                int   i=-1;
                while((i=instream.read())!=-1){
                    baos.write(i);
                }
                instream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return baos.toString();
    }

    //post 方式提交消息列队到指定url
    public boolean  SendMessage(String url){
            CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpGet  httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {

            response = httpclient.execute(httpGet);


            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                ByteArrayOutputStream baos   =   new   ByteArrayOutputStream();
                int   i=-1;
                while((i=instream.read())!=-1){
                    baos.write(i);
                }
                try {
                  //  System.out.println( baos.toString());
                    // do something useful
                } finally {
                    instream.close();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {


        }


        return true;
    }



}
